package com.qupai.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.constant.MessageConstant;
import com.qupai.constant.StatusConstant;
import com.qupai.dto.ActivitiesDTO;
import com.qupai.dto.ActivitiesPageQueryDTO;
import com.qupai.entity.Activities;
import com.qupai.exception.DeletionNotAllowedException;
import com.qupai.mapper.ActivitiesMapper;
import com.qupai.result.PageResult;
import com.qupai.service.ActivitiesService;
import com.qupai.vo.ActivitiesVO;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActivitiesServiceImpl extends ServiceImpl<ActivitiesMapper,Activities> implements ActivitiesService {
    @Resource
    private ActivitiesMapper activitiesMapper;

    /**
     * 新增活动
     */
    @Override
    public void save(ActivitiesDTO activitiesDTO) {
        Activities activities=new Activities();
        BeanUtils.copyProperties(activitiesDTO,activities);
        activities.setStatus(StatusConstant.DISABLE);

        activitiesMapper.insert(activities);
    }

    /**
     * 活动分页查询
     */
    @Override
    public PageResult pageQuery(ActivitiesPageQueryDTO activitiesPageQueryDTO) {
        //使用mybatisPlus自带的分页插件
        //mybatisX里面默认添加的分页插件和该插件只能存在一个，如果想转换必须导包
        //mybatisX里面的属性为int
        //mybatisPlus里面的属性为long

        // 1.构建分页条件
        Page<Activities> page=activitiesPageQueryDTO.toMpPageDefaultSortByCreateTimeDesc();

        // 2.分页查询
        Page<Activities> p=lambdaQuery()
                .like(activitiesPageQueryDTO.getName()!=null,Activities::getName,activitiesPageQueryDTO.getName())
                .eq(activitiesPageQueryDTO.getCollegesId()!=null,Activities::getCollegesId,activitiesPageQueryDTO.getCollegesId())
                .eq(activitiesPageQueryDTO.getStatus()!=null,Activities::getStatus,activitiesPageQueryDTO.getStatus())
                .page(page);

        // 3.调用插件获得数据
        //获得总的记录数
        long total = p.getTotal();
        //获得总的页数
        long pages = p.getPages();
        //获得当前页数据集合
        List<Activities> records = p.getRecords();

        return new PageResult(total,pages,records);
    }

    /**
     * 删除活动
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            Activities activities=activitiesMapper.selectById(id);
            //举办中的活动不能删除
            if (activities.getStatus().equals(StatusConstant.ENABLE) ){
                throw new DeletionNotAllowedException(MessageConstant.ACTIVITIES_ON_HOLD);
            }
        }
//        activitiesMapper.deleteByIds(ids);
        activitiesMapper.deleteBatchIds(ids);
    }

    /**
     * 根据id查询活动
     */
    @Override
    public ActivitiesVO getById(Long id) {
        Activities activities=activitiesMapper.selectById(id);
        ActivitiesVO activitiesVO=new ActivitiesVO();
        BeanUtils.copyProperties(activities,activitiesVO);
        return activitiesVO;
    }

    /**
     *  修改活动
     */
    @Override
    public void update(ActivitiesDTO activitiesDTO) {
        Activities activities=new Activities();
        BeanUtils.copyProperties(activitiesDTO,activities);
//        activitiesMapper.update(activities);
        activitiesMapper.updateById(activities);

    }

    /**
     *  启用或禁用活动
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Activities activities = Activities.builder()
                .id(id)
                .status(status)
                .build();
//        activitiesMapper.update(activities);
        activitiesMapper.updateById(activities);
    }

    /**
     * 根据各分类查询活动列表
     *
     * @param activities@return
     */
    @Override
    public List<ActivitiesVO> list(Activities activities) {
        List<Activities>activitiesList=activitiesMapper.list(activities);
        List<ActivitiesVO>activitiesVOList=new ArrayList<>();
        for (Activities activity:activitiesList){
            ActivitiesVO activitiesVO=new ActivitiesVO();
            //属性拷贝
            BeanUtils.copyProperties(activity,activitiesVO);
            activitiesVOList.add(activitiesVO);
        }
        return activitiesVOList;
    }
}
